home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / casetab.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-08-04  |  7.5 KB  |  264 lines

  1. /* XEmacs routines to deal with case tables.
  2.    Copyright (C) 1987, 1992, 1993, 1994 Free Software Foundation, Inc.
  3.  
  4. This file is part of XEmacs.
  5.  
  6. XEmacs is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by the
  8. Free Software Foundation; either version 2, or (at your option) any
  9. later version.
  10.  
  11. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  12. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14. for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with XEmacs; see the file COPYING.  If not, write to the Free
  18. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. /* Synched up with: FSF 19.28. */
  21.  
  22. /* Written by Howard Gayle.  See chartab.c for details. */
  23.  
  24. #include <config.h>
  25. #include "lisp.h"
  26. #include "buffer.h"
  27.  
  28. Lisp_Object Qcase_table_p;
  29. Lisp_Object Vascii_downcase_table, Vascii_upcase_table;
  30. Lisp_Object Vascii_canon_table, Vascii_eqv_table;
  31.  
  32. static void compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse);
  33.  
  34. DEFUN ("case-table-p", Fcase_table_p, Scase_table_p, 1, 1, 0,
  35.   "Return t iff ARG is a case table.\n\
  36. See `set-case-table' for more information on these data structures.")
  37.   (table)
  38.      Lisp_Object table;
  39. {
  40.   Lisp_Object down, up, canon, eqv;
  41.   down = Fcar_safe (table);
  42.   up = Fcar_safe (Fcdr_safe (table));
  43.   canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
  44.   eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
  45.  
  46. #define STRING256_P(obj) \
  47.   (STRINGP (obj) && string_char_length (XSTRING (obj)) == 256)
  48.  
  49.   return (STRING256_P (down)
  50.       && (NILP (up) || STRING256_P (up))
  51.       && ((NILP (canon) && NILP (eqv))
  52.           || (STRING256_P (canon)
  53.                   && (NILP (eqv) || STRING256_P (eqv))))
  54.       ? Qt : Qnil);
  55. }
  56.  
  57. static Lisp_Object
  58. check_case_table (Lisp_Object obj)
  59. {
  60.   REGISTER Lisp_Object tem;
  61.  
  62.   while (tem = Fcase_table_p (obj), NILP (tem))
  63.     obj = wrong_type_argument (Qcase_table_p, obj);
  64.   return (obj);
  65. }   
  66.  
  67. DEFUN ("current-case-table", Fcurrent_case_table, Scurrent_case_table, 0, 0, 0,
  68.   "Return the case table of the current buffer.")
  69.   ()
  70. {
  71.   Lisp_Object down, up, canon, eqv;
  72.   struct buffer *buf = current_buffer;
  73.   
  74.   down = buf->downcase_table;
  75.   up = buf->upcase_table;
  76.   canon = buf->case_canon_table;
  77.   eqv = buf->case_eqv_table;
  78.  
  79.   return Fcons (down, Fcons (up, Fcons (canon, Fcons (eqv, Qnil))));
  80. }
  81.  
  82. DEFUN ("standard-case-table", Fstandard_case_table,
  83.   Sstandard_case_table, 0, 0, 0,
  84.   "Return the standard case table.\n\
  85. This is the one used for new buffers.")
  86.   ()
  87. {
  88.   return Fcons (Vascii_downcase_table,
  89.         Fcons (Vascii_upcase_table,
  90.                Fcons (Vascii_canon_table,
  91.                   Fcons (Vascii_eqv_table, Qnil))));
  92. }
  93.  
  94. static Lisp_Object set_case_table (Lisp_Object table, int standard);
  95.  
  96.  
  97. DEFUN ("set-case-table", Fset_case_table, Sset_case_table, 1, 1, 0,
  98.   "Select a new case table for the current buffer.\n\
  99. A case table is a list (DOWNCASE UPCASE CANONICALIZE EQUIVALENCES)\n\
  100.  where each element is either nil or a string of length 256.\n\
  101. DOWNCASE maps each character to its lower-case equivalent.\n\
  102. UPCASE maps each character to its upper-case equivalent;\n\
  103.  if lower and upper case characters are in 1-1 correspondence,\n\
  104.  you may use nil and the upcase table will be deduced from DOWNCASE.\n\
  105. CANONICALIZE maps each character to a canonical equivalent;\n\
  106.  any two characters that are related by case-conversion have the same\n\
  107.  canonical equivalent character; it may be nil, in which case it is\n\
  108.  deduced from DOWNCASE and UPCASE.\n\
  109. EQUIVALENCES is a map that cyclicly permutes each equivalence class\n\
  110.  (of characters with the same canonical equivalent); it may be nil,\n\
  111.  in which case it is deduced from CANONICALIZE.")
  112.   (table)
  113.      Lisp_Object table;
  114. {
  115.   return set_case_table (table, 0);
  116. }
  117.  
  118. DEFUN ("set-standard-case-table",
  119.        Fset_standard_case_table, Sset_standard_case_table, 1, 1, 0,
  120.   "Select a new standard case table for new buffers.\n\
  121. See `set-case-table' for more info on case tables.")
  122.   (table)
  123.      Lisp_Object table;
  124. {
  125.   return set_case_table (table, 1);
  126. }
  127.  
  128. static Lisp_Object
  129. set_case_table (Lisp_Object table, int standard)
  130. {
  131.   Lisp_Object down, up, canon, eqv;
  132.   struct buffer *buf = current_buffer;
  133.  
  134.   check_case_table (table);
  135.  
  136.   down = Fcar_safe (table);
  137.   up = Fcar_safe (Fcdr_safe (table));
  138.   canon = Fcar_safe (Fcdr_safe (Fcdr_safe (table)));
  139.   eqv = Fcar_safe (Fcdr_safe (Fcdr_safe (Fcdr_safe (table))));
  140.  
  141.   if (NILP (up))
  142.     {
  143.       up = Fmake_string (make_number (256), make_number (0));
  144.       compute_trt_inverse (down, up);
  145.     }
  146.  
  147.   if (NILP (canon))
  148.     {
  149.       REGISTER Charcount i;
  150.  
  151.       canon = Fmake_string (make_number (256), make_number (0));
  152.  
  153.       /* Set up the CANON vector; for each character,
  154.      this sequence of upcasing and downcasing ought to
  155.      get the "preferred" lowercase equivalent.  */
  156.       for (i = 0; i < 256; i++)
  157.     set_string_char (XSTRING (canon), i,
  158.              string_char
  159.              (XSTRING (down),
  160.               (Charcount) string_char
  161.               (XSTRING (up),
  162.                (Charcount) string_char (XSTRING (down), i))));
  163.     }
  164.  
  165.   if (NILP (eqv))
  166.     {
  167.       eqv = Fmake_string (make_number (256), make_number (0));
  168.  
  169.       compute_trt_inverse (canon, eqv);
  170.     }
  171.  
  172.   if (standard)
  173.     {
  174.       Vascii_downcase_table = down;
  175.       Vascii_upcase_table = up;
  176.       Vascii_canon_table = canon;
  177.       Vascii_eqv_table = eqv;
  178.     }
  179.   else
  180.     {
  181.       buf->downcase_table = down;
  182.       buf->upcase_table = up;
  183.       buf->case_canon_table = canon;
  184.       buf->case_eqv_table = eqv;
  185.     }
  186.   return table;
  187. }
  188.  
  189. /* Given a translate table TRT, store the inverse mapping into INVERSE.
  190.    Since TRT is not one-to-one, INVERSE is not a simple mapping.
  191.    Instead, it divides the space of characters into equivalence classes.
  192.    All characters in a given class form one circular list, chained through
  193.    the elements of INVERSE.  */
  194.  
  195. static void
  196. compute_trt_inverse (Lisp_Object trt, Lisp_Object inverse)
  197. {
  198.   Charcount i = 0400;
  199.   Emchar c, q;
  200.  
  201.   while (--i)
  202.     set_string_char (XSTRING (inverse), i, (Emchar) i);
  203.   i = 0400;
  204.   while (--i)
  205.     {
  206.       if ((q = string_char (XSTRING (trt), i)) != (Emchar) i)
  207.     {
  208.       c = string_char (XSTRING (inverse), (Charcount) q);
  209.       set_string_char (XSTRING (inverse), (Charcount) q, (Emchar) i);
  210.       set_string_char (XSTRING (inverse), i, c);
  211.     }
  212.     }
  213. }
  214.  
  215.  
  216. void
  217. syms_of_casetab (void)
  218. {
  219.   defsymbol (&Qcase_table_p, "case-table-p");
  220.  
  221.   defsubr (&Scase_table_p);
  222.   defsubr (&Scurrent_case_table);
  223.   defsubr (&Sstandard_case_table);
  224.   defsubr (&Sset_case_table);
  225.   defsubr (&Sset_standard_case_table);
  226. }
  227.  
  228. void
  229. vars_of_casetab (void)
  230. {
  231.   REGISTER Emchar i;
  232.   Lisp_Object tem;
  233.  
  234.   staticpro (&Vascii_downcase_table);
  235.   staticpro (&Vascii_upcase_table);
  236.   staticpro (&Vascii_canon_table);
  237.   staticpro (&Vascii_eqv_table);
  238.  
  239. #if 0
  240.   DEFVAR_LISP ("ascii-downcase-table", &Vascii_downcase_table,
  241.            "String mapping ASCII characters to lowercase equivalents.");
  242.   DEFVAR_LISP ("ascii-upcase-table", &Vascii_upcase_table,
  243.            "String mapping ASCII characters to uppercase equivalents.");
  244. #endif
  245.  
  246.   tem = Fmake_string (make_number (256), make_number (0));
  247.   Vascii_downcase_table = tem;
  248.   Vascii_canon_table = tem;
  249.   
  250.   for (i = 0; i < 256; i++)
  251.     set_string_char (XSTRING (tem), (Charcount) i, tolower (i));
  252.   
  253.   tem = Fmake_string (make_number (256), make_number (0));
  254.   Vascii_upcase_table = tem;
  255.   Vascii_eqv_table = tem;
  256.   
  257.   for (i = 0; i < 256; i++)
  258.     set_string_char (XSTRING (tem), (Charcount) i, (isupper (i)
  259.                             ? tolower (i)
  260.                             : (islower (i)
  261.                                ? toupper (i)
  262.                                : i)));
  263. }
  264.